//
//  NetworkTool.m
//  268EDU_Demo
//
//  Created by 刘星 on 2017/8/14.
//  Copyright © 2017年 edu268. All rights reserved.
//

#import "NetworkTool.h"
#import "AFNetworking.h"

@implementation NetworkTool

+ (void)sendGetUrl:(NSString *)url withParams:(NSDictionary *)params success:(successBlock) success failure:(failureBlock)failure {
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer = [AFJSONResponseSerializer serializer];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    [manger GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}


+ (void)sendPostUrl:(NSString *)url withParams:(NSDictionary *)params success:(successBlock)success failure:(failureBlock)failure {
    [self printRequestUrlString:url withParamter:params];
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer = [AFJSONResponseSerializer serializer];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];//text/html
    [manger POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void)printRequestUrlString:(NSString *)urlString withParamter:(NSDictionary *)dic
{
    NSArray *dicKeysArray = [dic allKeys];
    NSString *urlWithParamterString = urlString;
    if (dicKeysArray.count != 0)
    {
        urlWithParamterString = [urlWithParamterString stringByAppendingString:@"?"];
    }
    for (NSInteger i = 0; i < dicKeysArray.count; i++)
    {
        urlWithParamterString = [urlWithParamterString stringByAppendingString:[NSString stringWithFormat:@"%@=%@&", dicKeysArray[i], [dic objectForKey:dicKeysArray[i]]]];
        if (i == dicKeysArray.count - 1)
        {
            urlWithParamterString = [urlWithParamterString substringToIndex:urlWithParamterString.length - 1];
        }
    }
    NSLog(@"\n\n路径--%@", urlWithParamterString);
}

@end
